<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\ShouXiangKouModel;
use app\index\relevan\EnginRelevan;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Piao extends Common
{
    public function detail_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.number|kai.shouer|kai.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['kaier']) && !empty($param['kaier'])){
            $where[] = ['kai.kaier','=', $param['kaier']];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['kai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }

        $data['kaier'] = (new UserService())->get_chaninfo_all();;
        $data['is_kaier'] = isset($param['kaier']) ? $param['kaier'] : '';


        $data['data'] = PiaoKaiListModel::alias('list')
            ->field('kai.*,e.name as enginname,bill.sericnum
            ,list.nums,list.fee as listfee,list.rate as listrate
            ,list.ratefee as listratefee,list.money as listmoney,list.explain as listexplain')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->join('engin e','e.id=kai.enginid')
            ->leftJoin('flow_bill bill','bill.id=kai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'kai'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['shuiqutype_name'] = (new PiaoService())->get_kai_shuiqutype_name($item['shuiqutype']);
                $item['kaier_name'] = DeptModel::get_name_by_pk($item['kaier']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['kai.state','=', $param['state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';

        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=kai.enginid')
            ->leftJoin('flow_bill bill','bill.id=kai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['shuiqutype_name'] = (new PiaoService())->get_kai_shuiqutype_name($item['shuiqutype']);
                $item['kaier_name'] = DeptModel::get_name_by_pk($item['kaier']);
                $item['state_name'] = $item['state'] > 0 ? '已开票' : '未开票';

                if($item['kouid'] > 0){
                    $item['shouXiang'] = ShouXiangKouModel::alias('list')
                        ->field('shou.id,shou.number,list.money')
                        ->join('shou_xiang shou','shou.id=list.shouid')
                        ->where(['list.id'=>$item['kouid']])
                        ->find();
                }

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_kai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_kai_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.number|kai.shouer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['shuiqutype']) && !empty($param['shuiqutype'])){
            $where[] = ['kai.shuiqutype','=', $param['shuiqutype']];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['kai.state','=', $param['state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['kaier']) && !empty($param['kaier'])){
            $where[] = ['kai.kaier','=', $param['kaier']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['kaier'] = (new UserService())->get_chaninfo_all();;
        $data['is_kaier'] = isset($param['kaier']) ? $param['kaier'] : '';


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';

        $data['shuiqutype'] = (new PiaoService())->get_kai_shuiqutype();
        $data['is_shuiqutype'] = isset($param['shuiqutype']) ? $param['shuiqutype'] : '';


        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=kai.enginid')
            ->leftJoin('flow_bill bill','bill.id=kai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai_kuan', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['shuiqutype_name'] = (new PiaoService())->get_kai_shuiqutype_name($item['shuiqutype']);
                $item['kaier_name'] = DeptModel::get_name_by_pk($item['kaier']);
                $item['state_name'] = $item['state'] > 0 ? '已开票' : '未开票';

                if($item['kouid'] > 0){
                    $item['shouXiang'] = ShouXiangKouModel::alias('list')
                        ->field('shou.id,shou.number,list.money')
                        ->join('shou_xiang shou','shou.id=list.shouid')
                        ->where(['list.id'=>$item['kouid']])
                        ->find();
                }

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_kai_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function kai_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择开票日期');
            }

            $param['state'] = 1;

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'piao_kai') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                PiaokaiModel::update($param);

                PiaoKaiListModel::where(['kaiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $param['id'];
                        PiaoKaiListModel::insert($item);
                    }
                }
                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                (new EnginRelevan())->vate_engin_file('piao_kai', $param['id']);


                (new OptService())->add_flow_log('开票', 'piao_kai', $param['id']);
                (new OptService())->add_log_list('开票', 'piao_kai', $param['id'], '项目开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
            $data['shiinfo'] = $shiinfo;


            $data['kouidinfo'] = ShouXiangKouModel::alias('list')
                ->field('shou.id,shou.number,list.id as listid,list.money')
                ->join('shou_xiang shou','shou.id=list.shouid')
                ->where(['list.id'=>$data['data']['kouid']])->find();

            $data['data']['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($data['data']['piaotype']);
            $data['data']['shuiqutype_name'] = (new PiaoService())->get_kai_shuiqutype_name($data['data']['shuiqutype']);
            $data['data']['kaier_name'] = DeptModel::where(['id'=>$data['data']['kaier']])->value('name');

            $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_piao_kai(Request $request){
        $param = $request->post();
        $data = PiaokaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaokaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_kai', $data['id'], '项目开票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_kai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaokaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['state_name'] = $data['data']['state'] > 0 ? '已开票' : '未开票';
        $data['data']['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($data['data']['piaotype']);
        $data['data']['shuiqutype_name'] = (new PiaoService())->get_kai_shuiqutype_name($data['data']['shuiqutype']);
        $data['data']['kaier_name'] = DeptModel::where(['id'=>$data['data']['kaier']])->value('name');
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['kouidinfo'] = ShouXiangKouModel::alias('list')
            ->field('shou.id,shou.number,list.id as listid,list.money')
            ->join('shou_xiang shou','shou.id=list.shouid')
            ->where(['list.id'=>$data['data']['kouid']])->find();



        $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_piao_shou(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['list.caiid','=', $param['caiid']];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['list.piaotype','=', $param['piaotype']];
        }
        if(isset($param['yongtype']) && !empty($param['yongtype'])){
            $where[] = ['list.yongtype','=', $param['yongtype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.title|shou.number|shou.shouer|list.gonger|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['caiid_name']) && !empty($param['caiid_name'])){
            $where[] = ['cai.name','like', '%'.$param['caiid_name'].'%'];
        }

        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }
        if(isset($param['isdikouid']) && $param['isdikouid'] != ''){
            if($param['isdikouid'] == '是'){
                $where[] = ['list.dikouid','>', 0];
            }
            if($param['isdikouid'] == '否'){
                $where[] = ['list.dikouid','=', 0];
            }
        }



        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';

        $data['yongtype'] = (new PiaoService())->get_piao_shou_yongtype();
        $data['is_yongtype'] = isset($param['yongtype']) ? $param['yongtype'] : '';


        $data['data'] = PiaoShouListModel::alias('list')
            ->field('shou.*,e.name as enginname,bill.sericnum
            ,list.caiid,list.gonger,list.dt as listdt,list.nums,list.piaotype,list.fee as listfee,list.rate as listrate
            ,list.ratefee as listratefee,list.money as listmoney,list.explain as listexplain,list.yongtype,list.dikouid')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'shou'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['yongtype_name'] = (new PiaoService())->get_piao_shou_yongtype_name($item['yongtype']);
                $item['dikouid_name'] = $item['dikouid'] > 0 ? '是' : '否';
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = PiaoShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,bill.sericnum,bill.modename')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_shou_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.title|shou.number|shou.shouer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shou.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = PiaoShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,bill.sericnum,bill.modename')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou_kuan', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_shou_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_shou(Request $request){
        $param = $request->post();
        $data = PiaoShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_shou', $data['id'], '项目收票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
            $item['yongtype_name'] = (new PiaoService())->get_piao_shou_yongtype_name($item['yongtype']);
        }
        $data['list'] = $list;

        $data['data']['money_zhuan'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.piaotype'=>1])
            ->sum('list.money');
        $data['data']['money_pu'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.piaotype'=>2])
            ->sum('list.money');
        $data['data']['money_cai'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.yongtype'=>'cai'])
            ->sum('list.money');
        $data['data']['money_ren'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.yongtype'=>'ren'])
            ->sum('list.money');
        $data['data']['money_ji'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.yongtype'=>'ji'])
            ->sum('list.money');
        $data['data']['money_fen'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.yongtype'=>'fen'])
            ->sum('list.money');
        $data['data']['money_qi'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.yongtype'=>'qi'])
            ->sum('list.money');


        $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_piao_tax(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tax.deleted_user','=', 0];
        $where[] = ['tax.comid','=',$this->userinfo['comid']];
        $where[] = ['tax.isturn','=',1];
        $where[] = ['tax.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['tax.enginid','=', $param['enginid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['tax.status','=', $param['status']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|tax.number|tax.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tax.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['tax.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['tax.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tax.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoTaxModel::alias('tax')
            ->field('tax.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=tax.enginid')
            ->leftJoin('flow_bill bill','bill.id=tax.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_tax', 'tax'))
            ->order('tax.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_tax');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_tax(Request $request){
        $param = $request->post();
        $data = PiaoTaxModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoTaxModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_tax', $data['id'], '税费预缴');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_tax(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoTaxModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = PiaoTaxListModel::where(['taxid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['ratetype'] = (new OptService())->get_base_showname('cai_ratetype', $item['ratetype']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_tax', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }





































}
